package aplicacao.campeonato;

import controle.Relogio;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;
import modelo.Luta;
import modelo.Parametros;
import modelo.excecoes.LutaComandoException;

/**
 * Display do tempo medico
 */
public class DisplayTempo extends javax.swing.JDialog implements Observer{
    
    /** */
    private long tempoDuracao = 0;
    
    /** Relogio */
    private Relogio relogio;
    
    /** Modo de operacao - 1 = tempo médico ou 2 = intervalo*/
    private int modoOperacao = 0;
    
    /**
     * Creates new form TempoMedico
     * @param parent Componente (JDialog) que abriu este display
     * @param modal Se este display será modal
     */
    public DisplayTempo(javax.swing.JDialog parent, boolean modal, 
            Luta lutaAtiva, int modoOperacao ) {
        
        super(parent, modal);
        initComponents();
        
        this.modoOperacao = modoOperacao;
        
        // Se for tempo médico
        if(this.modoOperacao == 1){
            jlTitulo.setText("Tempo Médico");
            try {
                lutaAtiva.pausar();
            } catch (LutaComandoException ex) {
                JOptionPane.showMessageDialog( this, "Não é possível " +
                        "iniciar um tempo médico com a luta pausada.", "Erro!",
                        JOptionPane.ERROR_MESSAGE);
                this.dispose();
            }
            
            // Setando a duração do tempo médico
            this.tempoDuracao = 
                    Parametros.getInstance().getDuracaoTempoMedico();
            
        }
        
        // Se for intervalo entre os rounds
        if(this.modoOperacao == 2){
            jlTitulo.setText("Intervalo");
            
            // Setando a duração do tempo médico
            this.tempoDuracao = 
                    Parametros.getInstance().getDuracaoIntervalo();
            
        }        

        // relogio para controlar o tempo da luta
        this.relogio = new Relogio();
        
        this.relogio.iniciar();
        
        this.relogio.addObserver(this);

        // Centralizando a tela
        this.setLocationRelativeTo(null);
        this.setVisible(true);        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpRelogioTempoMedico = new javax.swing.JPanel();
        jlRelogio = new javax.swing.JLabel();
        jbEncerrar = new javax.swing.JButton();
        jlTitulo = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jpRelogioTempoMedico.setBackground(new java.awt.Color(255, 255, 255));
        jpRelogioTempoMedico.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jlRelogio.setFont(new java.awt.Font("Arial", 0, 200)); // NOI18N
        jlRelogio.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlRelogio.setText("00:00");
        jlRelogio.setFocusable(false);

        javax.swing.GroupLayout jpRelogioTempoMedicoLayout = new javax.swing.GroupLayout(jpRelogioTempoMedico);
        jpRelogioTempoMedico.setLayout(jpRelogioTempoMedicoLayout);
        jpRelogioTempoMedicoLayout.setHorizontalGroup(
            jpRelogioTempoMedicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpRelogioTempoMedicoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlRelogio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jpRelogioTempoMedicoLayout.setVerticalGroup(
            jpRelogioTempoMedicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpRelogioTempoMedicoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlRelogio, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbEncerrar.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jbEncerrar.setText("Encerrar");
        jbEncerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEncerrarActionPerformed(evt);
            }
        });

        jlTitulo.setFont(new java.awt.Font("Arial", 1, 36)); // NOI18N
        jlTitulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlTitulo.setText("Tempo Médico");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jpRelogioTempoMedico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbEncerrar)
                        .addGap(232, 232, 232))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpRelogioTempoMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbEncerrar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbEncerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEncerrarActionPerformed
    this.fechar();
}//GEN-LAST:event_jbEncerrarActionPerformed

    /**
     * metodo chamado quando os objetos observadir enviam uma notificação
     * @param o O objeto observado
     * @param arg Parâmetro passado junto com a notificação
     */
    @Override
    public void update(Observable o, Object arg) {
        
        long tempo = (((Long) arg));
        
        if(tempo <= this.tempoDuracao){
            
            Format formatter = new SimpleDateFormat("mm:ss");
            jlRelogio.setText(formatter.format(new Date(tempo)));
            
        } else {
            this.fechar();
        }
        
    }
    
    private void fechar(){

        // Encerrando relogio
        this.relogio.stopRequest();

        // Fecha essa tela
        this.dispose();        
        
    }
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbEncerrar;
    private javax.swing.JLabel jlRelogio;
    private javax.swing.JLabel jlTitulo;
    private javax.swing.JPanel jpRelogioTempoMedico;
    // End of variables declaration//GEN-END:variables
    
}
